﻿Imports Configuration.Templating.Context
Imports Configuration.Templating.Scope
Imports Configuration.Templating
Imports Aricie.DNN.UI.Controls
Imports DotNetNuke.Services.Localization

Namespace Aricie.LuceneSearch.Settings.Templates

    Public Class TemplateItem
        Inherits AricieUserControlBase

        Public ReadOnly Property CurrentTemplateProvider As TemplateProvider
            Get
                Return TemplateProvider.FromSession(SessionStateUtility.GetHttpSessionStateFromContext(HttpContext.Current))
            End Get
        End Property

        Public Property ContentType() As String
            Get
                Return ViewState("Content").ToString
            End Get
            Set(ByVal value As String)
                ViewState("Content") = value
                lblContentName.Text = value
                lblContentName.Visible = Not String.IsNullOrEmpty(value)
            End Set
        End Property

        Private Property TemplatingContext() As DNNTemplatingContext
            Get
                Return DirectCast(ViewState("ITemplatingContext"), DNNTemplatingContext)
            End Get
            Set(ByVal value As DNNTemplatingContext)
                ViewState("ITemplatingContext") = value
                ContentType = value.ContentDisplayName
            End Set
        End Property

        Private Property TemplatingScope() As ITemplateProviderScope
            Get
                Return DirectCast(ViewState("ITemplateProviderScope"), ITemplateProviderScope)
            End Get
            Set(ByVal value As ITemplateProviderScope)
                ViewState("ITemplateProviderScope") = value
            End Set
        End Property

        Public Sub SetupTemplating(ByVal Context As DNNTemplatingContext, ByVal Scope As ITemplateProviderScope)
            TemplatingContext = Context
            TemplatingScope = Scope
            UpdateDisplay()
        End Sub


        Private Sub UpdateDisplay()
            tbTemplate.Text = CurrentTemplateProvider.GetValue(TemplatingScope, TemplatingContext)
            Dim ValueExists = CurrentTemplateProvider.ValueExists(TemplatingScope, TemplatingContext)

            tbTemplate.CssClass = IIf(ValueExists, "ValueDefined", "ValueUndefined").ToString()
            pnlToolbar.Attributes("class") = "TemplateToolbar " & tbTemplate.CssClass

            imgDefined.Visible = ValueExists
            imgUndefined.Visible = Not ValueExists
            imgDelete.Visible = ValueExists

            imgDefined.ToolTip = Localization.GetString("imgDefined.ToolTip", Me.LocalResourceFile, System.Threading.Thread.CurrentThread.CurrentUICulture.Name)
            imgUndefined.ToolTip = Localization.GetString("imgUndefined.ToolTip", Me.LocalResourceFile, System.Threading.Thread.CurrentThread.CurrentUICulture.Name)
            imgDelete.ToolTip = Localization.GetString("imgDelete.ToolTip", Me.LocalResourceFile, System.Threading.Thread.CurrentThread.CurrentUICulture.Name)
        End Sub

        Private Sub tbTemplate_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles tbTemplate.TextChanged
            CurrentTemplateProvider.SetValue(TemplatingScope, TemplatingContext, tbTemplate.Text)
            UpdateDisplay()
        End Sub

        Private Sub imgDelete_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgDelete.Click
            CurrentTemplateProvider.DeleteValue(TemplatingScope, TemplatingContext)
            UpdateDisplay()
        End Sub

        Public Sub DisableForTrial(ByVal TrialMode As Aricie.DNN.UserControlEditControl.TrialModeInformation)
            imgDelete.Enabled = False
            imgDelete.ToolTip = TrialMode.Message
            tbTemplate.Enabled = False
            tbTemplate.ToolTip = TrialMode.Message
        End Sub
    End Class
End Namespace